home *** CD-ROM | disk | FTP | other *** search
/ Top 200 Programs / Top 200 Programs.iso / Bob8 / THOMPSON / LIBERTY / PRODUCT / TUTORIAL.EXE / WK5CM1.TXT < prev    next >
Text File  |  1996-06-06  |  12KB  |  366 lines

  1. Week 5 course material
  2. Liberty BASIC programming course
  3. Copyright 1996 Shoptalk Systems
  4. All Rights Reserved
  5.  
  6. An Introduction to Programming Graphics
  7. ==============================================================================
  8.  
  9. One of the most interesting and useful things that can computers can do is
  10. create graphics.  These can be useful diagrams, page layouts, animations,
  11. beautiful works of art, etc.
  12.  
  13.  
  14. Kinds of Graphics Controls
  15. ==============================================================================
  16.  
  17. Liberty BASIC programs can display graphics using two different types of
  18. graphics controls.  The first is a graphics window.  We create a graphics
  19. window in our programs using an OPEN statement in the same fashion we create
  20. other kinds of windows:
  21.  
  22.     open "My Drawing Area" for graphics as #graphWin
  23.  
  24. We can also add one or more graphicbox controls to other windows, like so:
  25.  
  26.     graphicbox #main.graph, 10, 10, 150, 200
  27.     open "My Window" for window as #main
  28.  
  29. Both kinds of graphics controls understand the same set of graphics commands.
  30. Using the second approach can provide more than one drawing area, or it can
  31. be used to create a graphics area with other controls in a window or dialog
  32. box.
  33.  
  34.  
  35. Turtle Graphics
  36. ==============================================================================
  37.  
  38. In many computer graphics systems there is a way to draw using a pen and
  39. paper metaphor.  Because some versions of this system used real paper, and
  40. also a pen attached to a small robot that looked like a turtle (a
  41. hemisphere), this way of drawing is often referred to as turtle graphics.
  42.  
  43. The idea is that you send the turtle commands like:
  44.  
  45.    -raise the pen up from the paper
  46.    -lower the pen down to the paper
  47.    -move forward a specified distance, drawing if the pen is down
  48.    -turn a specific number of degrees
  49.  
  50. By stringing these kinds of drawing commands together, useful and meaningful
  51. things can be drawn.
  52.  
  53. Liberty BASIC includes support for this kind of drawing.  Here is a short
  54. list of the Liberty BASIC turtle graphics commands:
  55.  
  56.     up    - lift the pen up (don't draw)
  57.     down  - lower the pen down (draw)
  58.     home  - place the pen in the center of the graphics area
  59.     go    - move foreward in the current direction
  60.     goto  - go to a specified position
  61.     place - go to a specified position but don't draw
  62.     turn  - turn the turtle clockwise a specified number of degrees
  63.     north - cause the turtle to point north (straight up)
  64.     posxy - tell us what the location of the turtle is
  65.  
  66. Let's draw a box in the center of a graphics control:
  67.  
  68.     'draw a box
  69.     open "Draw A Box" for graphics as #draw
  70.     print #draw, "up"
  71.     print #draw, "home"
  72.     print #draw, "north"
  73.     print #draw, "go 50"
  74.     print #draw, "turn 90"
  75.     print #draw, "go 50"
  76.     print #draw, "down"
  77.     print #draw, "turn 90"
  78.     print #draw, "go 100"
  79.     print #draw, "turn 90"
  80.     print #draw, "go 100"
  81.     print #draw, "turn 90"
  82.     print #draw, "go 100"
  83.     print #draw, "turn 90"
  84.     print #draw, "go 100"
  85.     input r$
  86.  
  87. Run the program and look at the box we've drawn.  Now maximize the window.
  88. See how the box doesn't get redrawn?  This is because we haven't told Liberty
  89. BASIC that we're done drawing a sequence.  To make what we've drawn 'stick',
  90. we must send a FLUSH command.  Insert the following line before our INPUT
  91. statement:
  92.  
  93.     print #draw, "flush"
  94.  
  95. Now rerun the program and maximize the window.  You will see that the box is
  96. redrawn now whenever the window is resized or redrawn.
  97.  
  98.  
  99. Doing more with less
  100. ------------------------------------------------------------------------------
  101.  
  102. For what our box drawing program does it is quite long.  There are a couple
  103. of things we can do to shorten the program.  One is to print more than one
  104. command on a line.  Look at this:
  105.  
  106.     print #draw, "up"
  107.     print #draw, "home"
  108.     print #draw, "north"
  109.     print #draw, "go 50"
  110.     print #draw, "turn 90"
  111.     print #draw, "go 50"
  112.     print #draw, "down"
  113.  
  114. An equivalent line is:
  115.  
  116.     print #draw, "up ; home ; north ; go 50 ; turn 90 ; go 50 ; down"
  117.  
  118. See how we can print more than one command per line by inserting a semicolon
  119. in between each command.
  120.  
  121. Now we turn the turtle 90 degrees and go 100 pixels.  This happens four
  122. times:
  123.  
  124.     print #draw, "turn 90"
  125.     print #draw, "go 100"
  126.     print #draw, "turn 90"
  127.     print #draw, "go 100"
  128.     print #draw, "turn 90"
  129.     print #draw, "go 100"
  130.     print #draw, "turn 90"
  131.     print #draw, "go 100"
  132.  
  133. With this example we can combine the TURN and GO commands into one line with
  134. a semicolon, and we can use a loop to perform the action four times, like
  135. so:
  136.  
  137.     for x = 1 to 4
  138.         print #draw, "turn 90 ; go 100"
  139.     next x
  140.  
  141. Here is the complete program rewritten as above:
  142.  
  143.     'draw a box
  144.     open "Draw A Box" for graphics as #draw
  145.     print #draw, "up ; home ; north ; go 50 ; turn 90 ; go 50 ; down"
  146.     for x = 1 to 4
  147.         print #draw, "turn 90 ; go 100"
  148.     next x
  149.     print #draw, "flush"
  150.  
  151.     input r$
  152.  
  153. For fun, here is a short program that draws a spiral:
  154.  
  155.     'draw a spiral
  156.     open "Draw A Spiral" for graphics as #draw
  157.     print #draw, " home ; down"
  158.     for x = 1 to 100
  159.         print #draw, "turn 91 ; go "; x * 2
  160.     next x
  161.     print #draw, "flush"
  162.  
  163.     input r$
  164.  
  165. See how we take the value of x in our FOR/NEXT loop and use it in our GO
  166. command.  We do this to make each next line longer than the last, and this
  167. is what makes our spiral get bigger as we draw it.
  168.  
  169. IMPORTANT: Spaces are important in any command that we print to a graphics
  170. control.  If we remove the spaces, the command will not work, and it will
  171. not return an error.  For example, the following line will NOT work:
  172.  
  173.         print #draw, "turn 91 ; go"; x * 2
  174.  
  175. See how the space was removed after the GO command?  This small change is
  176. very important.
  177.  
  178.  
  179. More about FLUSH
  180. -----------------------------------------------------------------------------
  181.  
  182. FLUSH doesn't only make drawn items 'stick' so that they redraw.  It gives
  183. all the items drawn before that FLUSH a common number.  This is useful
  184. so that a collection of drawing elements can be referred to together.
  185.  
  186. Here is an example of how we might use this feature:
  187.  
  188.     'demonstrate the usefulness of flush
  189.     open "Draw A Circle" for graphics as #draw
  190.     print #draw, " home ; north ; down"
  191.     for x = 1 to 30
  192.         print #draw, "turn 12 ; go 10"
  193.         print #draw, "flush"
  194.     next x
  195.  
  196.     for x = 1 to 29 step 2
  197.         print #draw, "delsegment "; x
  198.     next x
  199.  
  200.     print #draw, "redraw"
  201.  
  202.     input r$
  203.  
  204. Notice how we flush after each TURN and GO command.  This give each line
  205. its own segment number.  Then we loop through the odd numbers 1, 3,
  206. 5, ... 29 so we can delete every other segment.  When we print the REDRAW
  207. command, it only redraws the segments we haven't deleted and we see a
  208. dashed effect.
  209.  
  210. This segmented graphics capability is used a lot in the FreeForm program
  211. (FF12.BAS) that is included with Liberty BASIC.
  212.  
  213. We can get the current segment number at any time while drawing by using
  214. the SEGMENT command, like so:
  215.  
  216.     'demonstrate the usefulness of flush
  217.     open "Draw A Circle" for graphics as #draw
  218.     print #draw, " home ; north ; down"
  219.     for x = 1 to 30
  220.         print #draw, "turn 12 ; go 10"
  221.         print #draw, "flush"
  222.         'get the current segment number and print it
  223.         print #draw, "segment"
  224.         input #draw, segId
  225.         print segId
  226.     next x
  227.  
  228.     for x = 1 to 29 step 2
  229.         print #draw, "delsegment "; x
  230.     next x
  231.  
  232.     print #draw, "redraw"
  233.  
  234.     input r$
  235.  
  236. The SEGMENT command does NOT get the number of the last segment flushed.
  237. Subtract 1 to get that number.
  238.  
  239.  
  240. Color Graphics
  241. -----------------------------------------------------------------------------
  242.  
  243. We can dress up our graphics by adding some color to them.  Let's look at
  244. two commands for doing this:
  245.  
  246.     'draw a spiral
  247.     open "Draw A Spiral" for graphics as #draw
  248.     print #draw, "fill blue"
  249.     print #draw, " home ; down ; color white"
  250.     for x = 1 to 100
  251.         print #draw, "turn 91 ; go "; x * 2
  252.     next x
  253.     print #draw, "flush"
  254.  
  255.     input r$
  256.  
  257. The FILL command (the line after our OPEN statement) causes our graphic
  258. control to be filled with the color blue.  Then our spiral is drawn as
  259. white by setting the turtle's pen to white in the next line.
  260.  
  261. Users running 65,000 or more color drivers on their machine can specify
  262. RGB values for colors.  Each color can be given a value from 0 to 255 with
  263. 0 being darkest and 255 being lightest.  Try this example:
  264.  
  265.     'draw a spiral
  266.     open "Draw A Spiral" for graphics as #draw
  267.     print #draw, "fill darkgray"
  268.     print #draw, " home ; down"
  269.     for x = 1 to 100
  270.         print #draw, "color "; x * 2 + 55; " 0 " ; 200 - x
  271.         print #draw, "turn 91 ; go "; x * 2
  272.     next x
  273.     print #draw, "flush"
  274.  
  275.     input r$
  276.  
  277. Here is a list of valid colors:
  278.  
  279.     black, blue, brown, cyan, darkblue, darkcyan, darkgray,
  280.     darkgreen, darkpink, darkred, green, lightgray, palegray,
  281.     pink, red, white, yellow
  282.  
  283.  
  284.  
  285. Line Thickness
  286. ------------------------------------------------------------------------------
  287.  
  288. So far everything we've drawn has been razor-thin.  Drawing with a bold line
  289. is simple matter.  The SIZE command is all that's needed.  Try this:
  290.  
  291.     'draw a box
  292.     open "Draw A Box" for graphics as #draw
  293.     print #draw, "up ; home ; north ; go 50 ; turn 90 ; go 50 ; down"
  294.     'use a big fat pen
  295.     print #draw, "size 10"
  296.     for x = 1 to 4
  297.         print #draw, "turn 90 ; go 100"
  298.     next x
  299.     print #draw, "flush"
  300.  
  301.     input r$
  302.  
  303. If no size is specified, the default is 1.
  304.  
  305.  
  306. Drawing Text with Graphics
  307. ------------------------------------------------------------------------------
  308.  
  309. Drawing text in a graphics control is simple.  Position the pen as desired
  310. and print the text with an backslash in front.  Take a look:
  311.  
  312.     'display some text
  313.     open "Text example" for graphics as #draw
  314.     print #draw, "place 50 50"
  315.     print #draw, "\Hello world!"
  316.     print #draw, "flush"
  317.  
  318.     input r$
  319.  
  320. Each backslash in printed text causes a new line to be printed.  So if you
  321. want to display text that has a backslash in it (like a file path), then
  322. begin your text with a vertical bar '|' like so:
  323.  
  324.     'display some text
  325.     open "Text example" for graphics as #draw
  326.     print #draw, "place 50 50"
  327.     print #draw, "|c:\config.sys"
  328.     print #draw, "flush"
  329.  
  330.     input r$
  331.  
  332. You can use the COLOR command to change the color of printed text.  Watch
  333. what happens when you also fill the window with some other color.
  334.  
  335.     'display some text
  336.     open "Text example" for graphics as #draw
  337.     print #draw, "fill red ; color blue"
  338.     print #draw, "place 50 50"
  339.     print #draw, "|c:\config.sys"
  340.     print #draw, "flush"
  341.  
  342.     input r$
  343.  
  344. To change this white backdrop to the be the same color as our FILL of red,
  345. we can use the BACKCOLOR command, like this:
  346.  
  347.     'display some text
  348.     open "Text example" for graphics as #draw
  349.     print #draw, "fill red ; color blue ; backcolor red"
  350.     print #draw, "place 50 50"
  351.     print #draw, "|c:\config.sys"
  352.     print #draw, "flush"
  353.  
  354.     input r$
  355.  
  356.  
  357. Challenge Exercise
  358. ------------------------------------------------------------------------------
  359.  
  360. Create a program named TIME.BAS that gets the current time from the TIME$()
  361. function and then draws a clock with an hour, minute and second hand.  The
  362. display should have a background color, and each hand should have its own
  363. color.  A button on the clock should cause the display to redraw with the
  364. current time when clicked on.  The program doesn't have to run continuously.
  365.  
  366.